package ru.usu.gv.utils.imports.dynamic;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author spupyrev
 * 26.10.2009
 */
public class EnronData extends BaseData
{
	private static final String DATA_PATH = BASE_PATH + "enron_trace\\";
	//private static final String DATA_PATH = "E:\\USU\\DynamicVis\\raw_data\\enron.trace.TO.notfiltered\\";
	private static int START_TIME = 915405660;
	private static int END_TIME = 1029453660;

	private List<Message> sg60d, sg365d, sg3000d;

	@Override
	protected void readSpanners() throws IOException
	{
		//sg
		//sg60d = readSGSpanners(60);
		//sg365d = readSGSpanners(365);
		//sg3000d = readSGSpanners(3000);
	}

	private List<Message> readSGSpanners(int period) throws IOException
	{
		List<Message> list = new ArrayList<Message>();
		for (int time = START_TIME; time < END_TIME; time += DAY * period)
		{
			String dayName = String.valueOf(time + DAY * period);
			while (dayName.length() < 10)
				dayName = "0" + dayName;
			String fileName = "vnc.txt." + String.valueOf(period) + "." + dayName + ".strongs_w.txt";
			FileInputStream file = new FileInputStream(DATA_PATH + String.valueOf(period) + "d\\" + fileName);

			String s;
			BufferedReader is = new BufferedReader(new InputStreamReader(file, "cp1251"));
			while ((s = is.readLine()) != null)
			{
				String[] tmp = s.split("[\\-]");
				if (tmp.length != 2)
					continue;

				list.add(new Message(time + DAY, Integer.parseInt(tmp[0]), Integer.parseInt(tmp[1])));
			}
			is.close();
		}

		System.out.println("Read " + list.size() + " sg-" + String.valueOf(period) + " spanners");
		return list;
	}

	public Map<Integer, Set<Integer>> getSGEdges(int period, int day)
	{
		if (period == 60)
			return getSpannerEdges(sg60d, day);
		else if (period == 365)
			return getSpannerEdges(sg365d, day);
		else if (period == 3000)
			return getSpannerEdges(sg3000d, day);
		else
			throw new RuntimeException("wrong period");
	}

	@Override
	protected String getTracePath()
	{
		return DATA_PATH + "enron.trace.txt";
	}

	@Override
	protected String getGenderPath()
	{
		return DATA_PATH + "enron.genders.txt";
	}

	@Override
	protected String getNamesPath()
	{
		return DATA_PATH + "enron.names.txt";
	}

	@Override
	public int getStartTime()
	{
		return START_TIME;
	}

	@Override
	protected int getEndTime()
	{
		return END_TIME;
	}

}
